package com.hw.one.collect.client;

import com.hw.one.collect.util.HashUtil;
import com.hw.one.collect.zzcity.channel.ZzCityChannelInitializer;
import com.hw.one.collect.zzcity.constants.ZzCityConstants;
import io.netty.channel.ChannelInitializer;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.apache.log4j.Logger;

/**
 * Created by qunxing.du on 2016/7/12.
 * Copyright 2016 汉威.智慧环保事业部 Limited.
 * All rights reserved.
 */
public class ChannelInitinalizerFactory {
    private static Logger log = Logger.getLogger(ChannelInitinalizerFactory.class);
    private static ChannelInitinalizerFactory channelInitinalizerFactory;

    private ChannelInitinalizerFactory() {
    }

    public static ChannelInitinalizerFactory newInstance() {
        if (channelInitinalizerFactory == null) {
            channelInitinalizerFactory = new ChannelInitinalizerFactory();
        }
        return channelInitinalizerFactory;
    }

    public synchronized ChannelInitializer  getChannelInitializer(String host, int port, String url, String params) {
        if (ZzCityConstants.HOST.equalsIgnoreCase(host) && ZzCityConstants.PORT == port) {
            log.debug("郑州大气通道初始器");
            return getZzCityChannelInitializer(params);
        }
        return null;
    }

    /**
     * 获取郑州市大气的通道
     * @param params
     * @return
     */
    private synchronized ChannelInitializer  getZzCityChannelInitializer(String params){
        //获取数据类型
        String dataService = DataServiceFactory.newInstance().getZzCityDataService(params);
        int key = HashUtil.getHashCodeByParams(params);
        return new ZzCityChannelInitializer(dataService,key);
    }

}
